package cn.mw.cmdb.processor;

import cn.mw.cmdb.entity.ItemNameMapperInfo;
import cn.mw.cmdb.enums.MonitoringServerType;
import cn.mw.cmdb.param.HostDetailDto;
import cn.mw.cmdb.param.ItemTotalInfoParam;
import cn.mw.cmdb.param.MwKeyValueParam;
import cn.mw.cmdb.param.MwMacros;
import cn.mw.cmdb.param.zabbixResult.ItemApplication;
import cn.mw.cmdb.param.zabbixResult.MWItemHistoryDTO;
import cn.mw.cmdb.param.zabbixResult.MwZabbixItemResultParam;
import cn.mw.cmdb.param.zabbixResult.ValuemapDto;
import cn.mw.cmdb.processor.prome.PrometheusProcessor;
import cn.mw.cmdb.processor.zabbix.ZabbixMonitorProcessor;
import cn.mw.microMonitorCommon.entity.common.MwTPServerTable;
import cn.mw.microMonitorCommon.entity.zabbix.ApplicationDTO;
import cn.mw.microMonitorCommon.entity.zabbix.MWZabbixAPIResult;
import cn.mw.microMonitorCommon.util.SpringUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

public abstract class MonitorProcessor {
    protected String groupPrefix;

    private final static String groupFlag = "[分组]";

    protected MonitorValue monitorValue;

    protected List<? extends MonitorValue> monitorValueList;

    protected MwTPServerTable mwTPServerTable;

    public static MonitorProcessor build(MwTPServerTable mwTPServerTable, List<? extends MonitorValue> monitorValueList) {
        MonitorProcessor processor = null;
        MonitoringServerType type = MonitoringServerType.valueOf(mwTPServerTable.getMonitoringServerType());
        switch (type) {
            case Zabbix:
                processor = new ZabbixMonitorProcessor();
                String prefix = SpringUtils.getPropertiesValue("cmdb.zabbixGroupPrefix");
                if (StringUtils.hasText(prefix)) {
                    processor.setGroupPrefix(groupFlag + prefix);
                }
                break;
            case Prometheus:
                processor = new PrometheusProcessor();
            default:
        }
        processor.setMwTPServerTable(mwTPServerTable);
        if (CollectionUtils.isNotEmpty(monitorValueList)) {
            processor.setMonitorValue(monitorValueList.get(0));
        }
        processor.setMonitorValueList(monitorValueList);
        return processor;
    }


    public abstract void monitor(MonitorParamContext context);

    public abstract void batchMonitor(MonitorParamContext context);

    public abstract void removeMonitor(MonitorParamContext context);

    public abstract List<MwMacros> getMacrosByTemplateId(MonitorParamContext context);

    public abstract MonitorGroupInfoList getGroupInfo(MonitorParamContext context);

    public abstract List<MwZabbixItemResultParam> getInstanceMonitorState(MonitorParamContext context);

    public abstract void updateHostState(MonitorParamContext context);

    public abstract void batchDelMonitorAssets(MonitorParamContext context);

    public abstract List<ItemApplication> getItemInfoList(MonitorParamContext context) throws Exception;

    public abstract List<MwKeyValueParam> getAllTemplateInfoByHostId(MonitorParamContext context);

    public abstract MWZabbixAPIResult historyGetByItemid(MonitorParamContext context);

    public abstract boolean itemCheckNow(MonitorParamContext context);

    public abstract MWZabbixAPIResult queryItemInfos(MonitorParamContext context);

    public abstract List<String> getDiscoveryIdByHostId(MonitorParamContext context);

    public abstract MonitorGroupInfoList createGroupInfo(MonitorParamContext context) throws Exception;

    public MwTPServerTable getMwTPServerTable() {
        return mwTPServerTable;
    }

    public void setMwTPServerTable(MwTPServerTable mwTPServerTable) {
        this.mwTPServerTable = mwTPServerTable;
    }

    public MonitorValue getMonitorValue() {
        return monitorValue;
    }

    public void setMonitorValue(MonitorValue monitorValue) {
        this.monitorValue = monitorValue;
    }

    public List<? extends MonitorValue> getMonitorValueList() {
        return monitorValueList;
    }

    public void setMonitorValueList(List<? extends MonitorValue> monitorValueList) {
        this.monitorValueList = monitorValueList;
    }

    public String getGroupPrefix() {
        return groupPrefix;
    }

    public void setGroupPrefix(String groupPrefix) {
        this.groupPrefix = groupPrefix;
    }

    public abstract void addAndUpdateItemNameMapper(MonitorParamContext context) throws Exception;

    public abstract List<MWItemHistoryDTO> getMonitorHistoryData(MonitorParamContext context);

    public abstract List<MWItemHistoryDTO> getMonitorTrendData(MonitorParamContext context);

    public abstract HostDetailDto getUptimeAndCpuMemoryInfo(MonitorParamContext context);
    public abstract void batchEditorProxyValue(MonitorParamContext context);

    public abstract List<MwZabbixItemResultParam> getItemIdByName(MonitorParamContext context);

    public abstract List<MWItemHistoryDTO> getAvailableByItemId(MonitorParamContext context);

    public abstract List getNames(MonitorParamContext context);

    public abstract List<ApplicationDTO> getApplication(MonitorParamContext context);

    public abstract List<ItemNameMapperInfo> getItemNameMapperList(ItemNameMapperInfo param) throws Exception;

    public abstract List<ApplicationDTO> getAllApplicationList(MonitorParamContext context);

    public abstract MWZabbixAPIResult getItemNameByAppName(MonitorParamContext context);

    public abstract List<String> getItemIdsByAppName(MonitorParamContext context);

    public abstract List<String> getVcenterRelationAssetsHostId(MonitorParamContext context);

    public abstract MWZabbixAPIResult getItemResultByItemName(MonitorParamContext context);
    public abstract MWZabbixAPIResult getItemResultByApplicationName(MonitorParamContext context);

    public abstract void updateHostIpInfo(MonitorParamContext context) throws Exception;

    public abstract MWZabbixAPIResult getItemInfoByAppNameAndIds(MonitorParamContext context);

    public abstract MWZabbixAPIResult getItemInfoByAppName(MonitorParamContext context);

    public abstract MWZabbixAPIResult getDRuleByHostId(MonitorParamContext context);

    public abstract List<ValuemapDto> getValueMapByIdList(MonitorParamContext context);

    public abstract List<ItemTotalInfoParam> getItemTotalInfo(MonitorParamContext context) throws Exception;
}
